package com.webshop.servlet;

import com.webshop.Dao.ProductDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

@WebServlet("/Random")
public class RandomServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = 0;
        Random ra = new Random();
        while(true){
            id = ra.nextInt(ProductDao.maxid());
            if (ProductDao.getItem(id)!=null){
                resp.sendRedirect("product.jsp?id="+id);
                return;
            }
        }
    }
}
